<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image src="../../static/img/logo.png"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="username" type="text" maxlength="11" placeholder="用户名/电话" :focus="isFocus"></wInput>
				<wInput v-model="password" type="password" maxlength="11" placeholder="密码"></wInput>
			</view>
			<wButton class="wbutton" text="登 录" :rotate="isRotate" @tap="startLogin" bgColor="var(--echo-main-color)"></wButton>
			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view>
			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		login
	} from '@/config/api.js';
	// let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button

	export default {
		data() {
			return {
				// 数据双向绑定
				username: '', //用户/电话
				password: '', //密码
				isRotate: false, //是否加载旋转
				isFocus: true // 是否聚焦
			};
		},
		components: {
			wInput,
			wButton
		},
		methods: {
			// 登陆
			startLogin(e) {
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.username.length == "") {
					// uni提示框
					uni.showToast({
						icon: 'error',
						position: 'bottom',
						title: '用户名不能为空'
					});
					return;
				}
				login({
					username: this.username,
					password: this.password
				}).then(res => {
					console.log(res)
					let {
						nickname,
						avatar,
						token,
						customerNo,
						// 是否设置了支付密码
						txPasswordStatus,
						id
					} = res.data
					if (res)
						// 存储需要的用户信息
						uni.setStorageSync('nickname', nickname);
						uni.setStorageSync('avatar', avatar);
						uni.setStorageSync('token', token);
						uni.setStorageSync('customerNo', customerNo);
						uni.setStorageSync('customerId', id);
					// 跳转到tabbar页面只能使用这个
						uni.switchTab({
						url: '/pages/home/home'
					});
					if(txPasswordStatus === 0){
						uni.setStorageSync('isOpenVip', true);
					}
				})
			},
		}
	}
</script>

<style>
	@import url("./css/main.css");
</style>
